/*
Created By VietRise on 16/6/2014
*/

#include "ScreenLoading.h"
#include "ScreenStart.h"

ScreenLoading::~ScreenLoading()
{
	CC_SAFE_RELEASE(_holePool);
	CC_SAFE_RELEASE(_mousePool);
	CC_SAFE_RELEASE(_hammerHit);
	CC_SAFE_RELEASE(_hammerMove);
	CC_SAFE_RELEASE(_mouseHide);
}

ScreenLoading::ScreenLoading()
{
	_screenSize = CCDirector::sharedDirector()->getWinSize();
	_countLetter = 0;

	createScreen();
	createPools();
	createActions();
}

CCScene* ScreenLoading::scene()
{
	CCScene* scene = CCScene::create();
	ScreenLoading* layer = ScreenLoading::create();
	scene->addChild(layer);
	return scene;
}

bool ScreenLoading::init()
{
	if (!CCLayer::init()) {
		return false;
	}

	// Run Actions
	// ---------HAMMER-------------
	_hammer->runAction(_hammerHit);
	_hammer->runAction(_hammerMove);
	// ---------MOUSE-------------
	CCSprite* mouse;
	for (int i = 0; i < _mousePool->count(); i++)
	{
		mouse = (CCSprite*)_mousePool->objectAtIndex(i);
		CCFiniteTimeAction* sequence = CCSequence::create(CCDelayTime::create(0.2f + i*0.4f),
															(CCSequence*)_mouseHide->copy()->autorelease(),
															NULL);
		mouse->runAction(sequence);
	}
	// ---------LABEL-------------
	CCFiniteTimeAction* sequence = CCSequence::create(CCDelayTime::create(0.2f), 
														CCCallFunc::create(this, callfunc_selector(ScreenLoading::setLetterLoading)), 
														NULL);
	
	_labelLoadingWhite->runAction(CCRepeatForever::create((CCActionInterval*)sequence));

	return true;
}

void ScreenLoading::createScreen()
{ 
	CCSprite* bgSprite = CCSprite::create("bg_loading.png");
	bgSprite->setPosition(ccp(_screenSize.width * 0.5f, _screenSize.height * 0.5f));
	this->addChild(bgSprite, kBackground, kSpriteBackground);

	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sheet_sprite.plist");
	_gameBatchNode = CCSpriteBatchNode::create("sheet_sprite.png", 20);
	this->addChild(_gameBatchNode, kMiddleground);

	
	_labelLoadingBlack = CCLabelBMFont::create("LOADING...", "showg.fnt", _screenSize.width * 0.6f);
	_labelLoadingBlack->setColor(ccc3(0, 0, 0));
	_labelLoadingBlack->setAnchorPoint(ccp(0.0f, 0.0f));
	_labelLoadingBlack->setPosition(ccp(_screenSize.width/2 - 200, _screenSize.height*0.6f));
	this->addChild(_labelLoadingBlack, kForeground);

	_labelLoadingWhite = CCLabelBMFont::create("", "showg.fnt", _screenSize.width * 0.6f);
	_labelLoadingWhite->setColor(ccc3(255, 255, 255));
	_labelLoadingWhite->setAnchorPoint(ccp(0.0f, 0.0f));
	_labelLoadingWhite->setPosition(_labelLoadingBlack->getPosition());
	this->addChild(_labelLoadingWhite, kForeground);
}

void ScreenLoading::createPools()
{
	int i;
	// ------------------- HOLE -----------------------------
	_holePool = CCArray::createWithCapacity(5);
	_holePool->retain();
	CCSprite* sprite;
	CCPoint position;
	for (i = 0; i < 5; i++)
	{
		position = ccp(_screenSize.width*0.2f + _screenSize.width*i*0.15f, _screenSize.height*0.3);
		sprite = CCSprite::createWithSpriteFrameName("obj_hole_loading.png");
		sprite->setPosition(position);
		_holePool->addObject(sprite);
		_gameBatchNode->addChild(sprite, kBackground);
	}

	// ------------------- MOUSE -----------------------------
	_mousePool = CCArray::createWithCapacity(5);	
	_mousePool->retain();
	for (i = 0; i < 5; i++)
	{
		position = ccp(_screenSize.width*0.2 + _screenSize.width*i*0.15, _screenSize.height*0.3 - 5);
		sprite = CCSprite::createWithSpriteFrameName("obj_mouse.png");
		sprite->setAnchorPoint(ccp(0.5f, 0.1f));
		sprite->setPosition(position);
		_mousePool->addObject(sprite);
		_gameBatchNode->addChild(sprite, kMiddleground);
	}
	
	// ------------------- HAMMER -----------------------------
	_hammer = CCSprite::createWithSpriteFrameName("obj_hammer.png");
	_hammer->setAnchorPoint(ccp(0.25f, 0.5f));
	_hammer->setPosition(ccp(_screenSize.width*0.05f - 35, _screenSize.height*0.45));
	_hammer->setRotation(-20);
	_gameBatchNode->addChild(_hammer);
}


void ScreenLoading::createActions()
{
	// Action Hammer
	CCFiniteTimeAction* hammerHit = CCSequence::create(	CCEaseInOut::create(CCRotateTo::create(0.2f, -35), 2),
														CCEaseInOut::create(CCRotateTo::create(0.2f, 35), 2),
														NULL);
	_hammerHit = CCRepeatForever::create((CCActionInterval*)hammerHit);
	_hammerHit->retain();

	_hammerMove = CCMoveTo::create(2.4f, ccp(_screenSize.width*0.95f - 20, _hammer->getPositionY()));
	_hammerMove->retain();

	// Action Mouse
	_mouseHide = CCSequence::create(CCScaleTo::create(0.2f, 1.0f, 0.1f),
									CCCallFuncN::create(this, callfuncN_selector(ScreenLoading::mouseHide)),
									NULL);
	_mouseHide->retain();	
}

void ScreenLoading::mouseHide(CCNode* pSender)
{
	pSender->setVisible(false);
	pSender->setScaleY(1.0f);	
}

void ScreenLoading::setLetterLoading()
{
	_countLetter++;
	switch (_countLetter)
	{
	case 1:
		_labelLoadingWhite->setString("L");
		break;
	case 2:
		_labelLoadingWhite->setString("LO");
		break;
	case 3:
		_labelLoadingWhite->setString("LOA");
		break;
	case 4:
		_labelLoadingWhite->setString("LOAD");
		break;
	case 5:
		_labelLoadingWhite->setString("LOADI");
		break;
	case 6:
		_labelLoadingWhite->setString("LOADIN");
		break;
	case 7:
		_labelLoadingWhite->setString("LOADING");
		break;
	case 8:
		_labelLoadingWhite->setString("LOADING.");
		break;
	case 9:
		_labelLoadingWhite->setString("LOADING..");
		break;
	case 10:
		_labelLoadingWhite->setString("LOADING...");
		break;
	default:
		// Chuyen Man Hinh
		CCScene* newScene = CCTransitionMoveInR::create(0.2f, ScreenStart::scene());
		CCDirector::sharedDirector()->replaceScene(newScene);
		break;
	}
}